import {getValueFromUrl} from "./utils.js"

function resetPassword() {
    const password = $("#password").val()
    const password2 = $("#confirm_password").val()

    $.ajax({
        url: "http://www.woniu-news.com:8082/resetPassword.php",
        type: "POST",
        xhrFields: {
            withCredentials: true
        },
        data: {
            uid,
            password,
            password2
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                window.location.href = "user_management.html"
            } else if (resp.code === 401) {
                alert(resp.message)
                window.location.href = "index.html"
            } else {
                alert(resp.message)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}

const uid = getValueFromUrl("uid")
if (!uid) {
    alert("请选择要重置密码的用户")
    window.location.href = "index.html"
}

// 监听密码重置事件
$(".reset_password").click(function () {
    resetPassword()
})